package com.woniuxy.couponassembly.service.impl;

import com.woniuxy.couponassembly.model.Coupon;
import com.woniuxy.couponassembly.dao.CouponMapper;
import com.woniuxy.couponassembly.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Liyfi
 * @since 2021-05-31
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {
    @Autowired
    private CouponMapper mapper;

    @Override
    public int addCoupon(Coupon coupon) {
        int i = mapper.addCoupon(coupon);
        return i;
    }

    @Override
    public int getCouponId(int couponId) {
        int i = mapper.getLimitNum(couponId);
        return i;
    }

    @Override
    public int getIssuedQuantity(int couponId) {
        int issuedQuantity = mapper.getIssuedQuantity(couponId);
        return issuedQuantity;
    }

    @Override
    public int findCoupon(int couponId) {
        Coupon coupon = mapper.findCoupon(couponId);
        Date endDate = coupon.getEndDate();
        Date date = new Date();
        long time = date.getTime();
        long endDateTime = endDate.getTime();
        if(time - endDateTime >= 0){
            //返回1说明优惠券已经过期
            return 1;
        }else {
            //返回2说明优惠券没过期
                return 2;
        }


    }

    @Override
    public Coupon selCoupon(int couponId) {
        Coupon coupon = mapper.findCoupon(couponId);
        return coupon;
    }


}
